/*
 * $Id: common.h,v 1.1 2004/12/21 13:54:45 tsuruoka Exp $
 */

#ifndef __POSTAGGER_COMMON_H_
#define __POSTAGGER_COMMON_H_

#include <string>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <stdlib.h>
#include <sstream>

struct Token
{
	std::string str;
	std::string pos;
	std::string prd;
	std::string cprd;// for chunking
	std::string tag; // for chunking
	std::string ne;
	Token(std::string s, std::string p) : str(s), pos(p) {}
};

typedef std::vector<Token> Sentence;

static std::string  getStdoutFromCommand(std::string cmd)
{
	// setup
	std::string data;
	FILE *stream;
	char buffer[1000];

	// do it
	stream = popen(cmd.c_str(), "r");
	while ( fgets(buffer, 1000, stream) != NULL)
		data.append(buffer);
	pclose(stream);

	// exit
	return (data);
}



static void cleanvector(std::vector<std::string> * input)
{
	for(int i=0;i<input->size();i++)
	{
		input->at(i).erase(0,input->at(i).size());
		input->erase(input->begin()+i);
	}
	input->clear();
}

static void split(std::string text,std::string delmiter,std::vector<std::string>* result)
{
	std::string temp="";
	int delmiterlen=delmiter.size();
	for(int i=0;i<text.size();i++)
	{
		if(text.substr(i,delmiterlen).compare(delmiter)==0)
		{
			if(temp.compare("")!=0)
			{
				result->push_back(temp);
			}
			temp="";
			i+=delmiterlen-1;
		}
		else
		{
			temp+=text[i];
		}
	}
	if(temp.compare("")!=0)
	{
		result->push_back(temp);
	}
}
static void split(const std::string& s, char c,std::vector<std::string>& v)
{
	std::string::size_type i = 0;
	std::string::size_type j = s.find(c);
	if(j == -1) // if only string is one word or no delimeter found
	{
		v.push_back(s);
		return;
	}
	while (j != std::string::npos) {
		v.push_back(s.substr(i, j-i));
		i = ++j;
		j = s.find(c, j);
		if (j == std::string::npos)
			v.push_back(s.substr(i, s.length( )));
	}
}
static std::string intToString(int x)
{
	std::stringstream Num;
	std::string str;
	Num << x;
	str = Num.str();
	return str;

}


static std::string toLower(std::string input)
{
	std::string result="";
	for(int i=0;i<input.length();i++)
	{
		result+=tolower(input[i]);
	}
	return result;
}


#endif

/*
 * $Log: common.h,v $
 * Revision 1.1  2004/12/21 13:54:45  tsuruoka
 * add bidir.cpp
 *
 */
